package com.tomtop.config;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component;

import com.tomtop._const.Const;
import com.tomtop.core.Context;
import com.tomtop.core.utils.NumberUtils;
import com.tomtop.core.utils.StringUtils;

@Component
@ConfigurationProperties(prefix = "spring.application")
@EnableConfigurationProperties(WebSiteListConfig.class)
public class WebSiteConfig implements InitializingBean {
	private static Logger log = LoggerFactory.getLogger(WebSiteConfig.class);

	@Override
	public void afterPropertiesSet() throws Exception {
		log.info("当前执行的程序是 : {} {}", WebSiteConfig.getAppName(), WebSiteConfig.getVersion());
		log.info("当前站点静态资源版本号 : {} ", WebSiteConfig.getResourceVersion());
	}

	private static String version = "0.0.0";
	private static String home = "/";
	private static String appname = "";
	private static String resourceVersion = "";

	public static String getVersion() {
		return version;
	}

	public void setVersion(String version) {
		WebSiteConfig.version = version;
	}

	public static String getResourceVersion() {
		return resourceVersion;
	}

	public void setResourceVersion(String resourceVersion) {
		WebSiteConfig.resourceVersion = resourceVersion;
	}

	public static String getHome() {
		return home;
	};

	public void setHome(String home) {
		WebSiteConfig.home = home;
	}

	public static String getAppName() {
		return appname;
	}

	public void setName(String appname) {
		WebSiteConfig.appname = appname;
	}

	public static String getPrefix() {
		if (Context.isPc())
			return getWebSite().getPrefix().getPc();
		return getWebSite().getPrefix().getM();
	}

	public static boolean isContain() {
		String hostname = Context.getHostName();
		return hostname != null && WebSiteListConfig.getSiteList().containsKey(hostname);
	}

	public static String getHostName() {
		if (isContain()) {
			return Context.getHostName();
		}
		log.warn("请注意站点不存在,将使用默认站点!  {} ", Context.getContext());
		return WebSiteListConfig.getDefault();
	}

	public static String getLoginBreak(String backUrl) {
		String url;
		if (Context.isPc())
			url = WebSiteConfig.getWebSite().getUrl("login-and-break");
		else
			url = WebSiteConfig.getWebSite().getUrl("m-login-and-break");
		if (url == null)
			return "";
		if (backUrl == null)
			backUrl = "";
		else
			try {
				backUrl = URLEncoder.encode(backUrl, Const.DEFAULT_CHARSET.name());
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		return url.replace("$backUrl", backUrl);
	}

	public static String getVhost() {
		return getPrefix() + "." + getWebSite().getHost();
	}

	public static String getShopHome() {
		if (Context.isApp())
			return WebSiteConfig.getWebSite().getUrl("app-gohome");
		if (Context.isPc())
			return WebSiteConfig.getWebSite().getUrl("shop-home");
		return WebSiteConfig.getWebSite().getUrl("m-shop-home");
	}

	public static String getCartHome() {
		if (Context.isApp())
			return WebSiteConfig.getWebSite().getUrl("app-gocart");
		return "//" + Context.getHost();
	}

	public static String getStaticResourcePrefix() {
		if (!Context.isPc())
			return WebSiteConfig.getWebSite().getUrl("m-cdn-url");
		return WebSiteConfig.getWebSite().getUrl("cdn-url");
	}

	public static String getCommonJsPrefix() {
		if (!Context.isPc())
			return WebSiteConfig.getWebSite().getUrl("m-common-cdn-url");
		return WebSiteConfig.getWebSite().getUrl("common-cdn-url");
	}

	public static WebSite getWebSite() {
		String siteName = getHostName();
		return WebSiteListConfig.getSiteList().get(siteName);
	}

	public static WebSite getDefaultWebSite() {
		String siteName = WebSiteListConfig.getDefault();
		return WebSiteListConfig.getSiteList().get(siteName);
	}

	public static String getParam(String name, String defaultvalue) {
		String siteName = getHostName();
		WebSite webSite = WebSiteListConfig.getSiteList().get(siteName);
		if (webSite == null)
			return defaultvalue;
		Properties properties = webSite.getProperties();
		if (properties != null && properties.containsKey(name))
			return properties.getProperty(name);
		return defaultvalue;
	}

	public static String getParam(String name) {
		return getParam(name, null);
	}

	public static Double getParamDouble(String name, Double defaultvalue) {
		String param = getParam(name);
		if (StringUtils.isEmpty(param) || !NumberUtils.isDouble(param))
			return defaultvalue;
		return Double.parseDouble(param);
	}

	public static Double getParamDouble(String name) {
		return getParamDouble(name, null);
	}

	public static Long getParamLong(String name, Long defaultvalue) {
		String param = getParam(name);
		if (StringUtils.isEmpty(param) || !NumberUtils.isNumber(param))
			return defaultvalue;
		return Long.parseLong(param);
	}

	public static Long getParamLong(String name) {
		return getParamLong(name, null);
	}

	@SuppressWarnings("static-access")
	public static <T extends Enum<T>> T getParaEnum(String name, T defaultvalue) {
		if (defaultvalue == null)
			throw new RuntimeException("从参数获取枚举，默认值不可以为空");
		String enumStr = getParam(name);
		if (enumStr == null)
			return defaultvalue;
		try {
			@SuppressWarnings("unchecked")
			Class<T> cls = (Class<T>) defaultvalue.getClass();
			return defaultvalue.valueOf(cls, enumStr);
		} catch (Throwable e) {
			return defaultvalue;
		}
	}
}
